package com.burgos.inventory;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import android.widget.TextView;
import android.widget.Toast;

public class BarScanner extends Activity implements OnClickListener {

	private Button search;
	private ProgressDialog pDialog;
	private String codename;
	private String BFormat;
	private TextView t, r;
	private JSONParser jsonParser = new JSONParser();
	private static final String LOGIN_URL = "http://test1984.site11.com/remote/searchproduct.php";
	private static final String TAG_SUCCESS = "success";
	private static final String TAG_MESSAGE = "message";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.scan_bar);
		//Get parameters from previous activity 
		codename = getIntent().getExtras().getString("keyword");
		BFormat = "Format: " + getIntent().getExtras().getString("format");
		//View (print) the barcode number and type on the layout
		t=new TextView(this); 
	    t=(TextView)findViewById(R.id.inputbarcode); 
	    t.setText(codename);    
	    r=new TextView(this); 
	    r=(TextView)findViewById(R.id.scanFormat); 
	    r.setText(BFormat);		
		search = (Button) findViewById(R.id.gosearch2);
		search.setOnClickListener(this);
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.gosearch2:
			new TrySearch().execute();
			break;
		default:
			break;
		}
	}
	
	class TrySearch extends AsyncTask<String, String, String> {

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			pDialog = new ProgressDialog(BarScanner.this);
			pDialog.setMessage("Searching Product, please wait...");
			pDialog.setIndeterminate(false);
			pDialog.setCancelable(true);
			pDialog.show();
		}

		@Override
		protected String doInBackground(String... args) {
			//In case no number/code were displayed or detected
			int success;
			if(codename==""){
				return "Error";
			}
			try {
				List<NameValuePair> params = new ArrayList<NameValuePair>();
				params.add(new BasicNameValuePair("keyword", codename));
				//Make the http request sending the code as parameter
				JSONObject json = jsonParser.makeHttpRequest(LOGIN_URL, "POST",	params);
				success = json.getInt(TAG_SUCCESS);
				if (success == 1) {
					//If succeed pass the code to the enxt activity to retrieve the data
					Intent i = new Intent(BarScanner.this, Product.class);
					i.putExtra("keyword",codename);
					finish();
					startActivity(i);
				} else {
					return json.getString(TAG_MESSAGE);
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
			return null;
		}

		protected void onPostExecute(String file_url) {
			pDialog.dismiss();
			if (file_url != null) {
				Toast.makeText(BarScanner.this, file_url, Toast.LENGTH_LONG).show();
			}
		}
	}
}
